/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author shonak
 */
public class TestConnexion {
    
    public static void main(String[] args){
        
        java.sql.Connection connexion = null;
        java.sql.Statement instruction = null;
        java.sql.ResultSet resultat = null;
        
        try{
            java.lang.Class.forName("com.mysql.jdbc.Driver");
            connexion = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test","root","shonaka123z321");
            instruction = connexion.createStatement();
            
            java.sql.DatabaseMetaData metadonnes = connexion.getMetaData();
            
           
            
            instruction.executeUpdate("DROP TABLE facture");
            instruction.executeUpdate("CREATE TABLE facture(client VARCHAR(50),produit VARCHAR(100),prix DECIMAL(9,2))");
            instruction.executeUpdate("INSERT INTO facture values('Konan','Savon BF','2500')");
            instruction.executeUpdate("INSERT INTO facture values('Konan','Lait Bonnet Rouge','12500')");
            instruction.executeUpdate("INSERT INTO facture values('Akissi','Pagne WAX','25000')");
            
            String produits="";
            Double totalfacture = 0d;
            
            resultat = instruction.executeQuery("SELECT * FROM facture where client ='Konan'");
            
            while(resultat.next()){
                produits += resultat.getString("produit")+" ";
                totalfacture += resultat.getDouble("prix");
            }
            
            javax.swing.JOptionPane.showMessageDialog(null,"Les produits suivants :"+ produits
                    + "\n coûtent "+ totalfacture);
            
        }catch(java.lang.ClassNotFoundException ex){
            javax.swing.JOptionPane.showMessageDialog(null,"Fichier introuvable : "+ex.getMessage());
        }
        catch(java.sql.SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(null,"Connexion impossible : "+ex.getMessage());
            
        }finally{
            try{
                if (connexion != null)
                     connexion.close();
                if (instruction != null)
                     instruction.close();
                if (resultat != null)
                     resultat.close();
                
            }catch(java.sql.SQLException ex) {
             javax.swing.JOptionPane.showMessageDialog(null,"Connexion impossible : "+ex.getMessage());
            
             }
        }
        
    }
    
    
}
